﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.PhotoWall
{
    public partial class PhotoWallList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载

        public void BindData()
        {
            this.ppBranch.WindinsUrl = "../../../Share/Picker/BranchSinglePicker.aspx?BranchID=" + BranchID;
            this.ppBranch.Text = ControlBusiness.GetBranchPickerText(BranchID);
            this.ppBranch.Value = BranchID;
            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
        }

        private void LoadData()
        {
            // 权限检查
            //CheckPowerWithButton("PhotoWallAdd", btnAdd);
            ControlBusiness.ControlBindDataDictionary(ddlCategory, "ZHDJ.PhotoWall.Category", true);

            btnAdd.OnClientClick = Dialog.GetShowReference("PhotoWallEdit.aspx", "新增图片库");



            BindGrid();
        }

        private void BindGrid()
        {
            var ds = PartyPhotoWall.GetPartyPhotoWallData(Grid1.PageIndex, Grid1.PageSize, txtTitle.Text.Trim(), ppBranch.Value == "" ? BranchID : ppBranch.Value, ddlStatus.SelectedValue, txtCreateBy.Text.Trim(), ddlCategory.SelectedValue);

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("PhotoWallEdit", Grid1, "editField");
            //CheckPowerWithLinkButtonField("PhotoWallDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string wallId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                //if (!CheckPower("PhotoWallDelete"))
                //{
                //    CheckPowerFailWithAlert();
                //    return;
                //}

                var count = DB.PartyPhotoWalls.Count(t => t.ID == wallId && t.Status == 4);
                if (count > 0)
                {
                    Alert.ShowInTop("该图片库已发布，无法删除！");
                    return;
                }

                if (DB.PartyPhotoWallImages.Count(t => t.PhotoWallID == wallId) > 0)
                {
                    var delImage = DB.PartyPhotoWallImages.FirstOrDefault(t => t.PhotoWallID == wallId);
                    DB.PartyPhotoWallImages.Remove(delImage);
                    DB.SaveChanges();
                }

                var del = DB.PartyPhotoWalls.FirstOrDefault(t => t.ID == wallId);
                DB.PartyPhotoWalls.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }

        public static string GetBranchName(string branchId)
        {
            return ControlBusiness.GetBranchPickerText(branchId);
        }

        public string GetCategory(string category)
        {
            DataTable dt = PartyPhotoWall.GetCategory("ZHDJ.PhotoWall.Category");
            if (dt.Rows.Count > 0)
            {
                DataRow dataRow = dt.Select("DDicMember='" + category + "'")[0];
                return Convert.ToString(dataRow["DDicMemberName"]);
            }
            else
            {
                return "";
            }
        }

        #endregion

        #region 按钮事件

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void txtTitle_Trigger2Click(object sender, EventArgs e)
        {
            //txtTitle.s = true;
            BindGrid();
        }

        protected void txtTitle_Trigger1Click(object sender, EventArgs e)
        {
            txtTitle.Text = string.Empty;
            //txtTitle.ShowTrigger1 = false;
            BindGrid();
        }

        protected void txtCreateBy_Trigger2Click(object sender, EventArgs e)
        {
            //txtCreateBy.ShowTrigger1 = true;
            BindGrid();
        }

        protected void txtCreateBy_Trigger1Click(object sender, EventArgs e)
        {
            txtCreateBy.Text = string.Empty;
            //txtCreateBy.ShowTrigger1 = false;
            BindGrid();
        }

        protected void ppBranch_Trigger1Click(object sender, EventArgs e)
        {
            ppBranch.Value = "";
            ppBranch.Text = "";
            ppBranch.ShowTrigger1 = false;
        }

        protected void ppBranch_TextChanged(object sender, EventArgs e)
        {
            ppBranch.ShowTrigger1 = !string.IsNullOrEmpty(ppBranch.Text);
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            //if (!CheckPower("PhotoWallDelete"))
            //{
            //    CheckPowerFailWithAlert();
            //    return;
            //}

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            //删除图片
            DB.PartyPhotoWallImages.Where(u => ids.Contains(u.PhotoWallID)).ToList().ForEach(u => DB.PartyPhotoWallImages.Remove(u));
            DB.SaveChanges();

            // 执行数据库操作
            DB.PartyPhotoWalls.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyPhotoWalls.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            BindGrid();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void CommentDialog_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
    }
}